// miniprogram/pages/index/regist/regist.js
import {
  getUUID,
  getExt
} from "../../../utils/utils.js";
const db = wx.cloud.database();
const _ = db.command;
Page({

  data: {
    regBtn: false,
    company_obj: {},
    focusWhat: 0,
    temImgs: [],
    cpny_deadline: null
  },

  next: function (event) {
    const x = event.target.dataset.x;

    this.setData({
      focusWhat: x
    })
  },
  onImgAddTap: function () {
    const that = this;



    wx.chooseImage({
      count: 1,
      success: function (res) {
        const temImgs = res.tempFilePaths;
        const oldImgs = that.data.temImgs;
        const newImgs = oldImgs.concat(temImgs);
        that.setData({
          temImgs: newImgs
        })
      },
    })
  },
  onCloseImgTap: function (event) {
    const index = event.target.dataset.index;
    const temImgs = this.data.temImgs;
    temImgs.splice(index, 1);
    this.setData({
      temImgs: temImgs
    })
  },
  onImgTap: function (ev) {
    const that = this;
    const index = ev.target.dataset.index;
    const current = that.data.temImgs[index];
    wx.previewImage({
      urls: that.data.temImgs,
      current: current
    })
  },
  setDate: function (event) {
    this.setData({
      cpny_deadline: event.detail.value
    })
  },
  check: function(event){
    const that = this;
    let tem="";
    wx.requestSubscribeMessage({
      tmplIds: ['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'], 
      success: res=>{
        tem = res['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'];
        if(tem=="accept"){
            wx.showToast({
            title: '已开启',
          })
          that.regist(event);
        }else{
          wx.showToast({
            title: '已拒绝',
          })
        }
      },
      fail: res=>{
          wx.showToast({
            title: '已拒绝',
          })
      }
    })
  },
  regist: async function (event) {
    const that = this;
    const cpny_name = event.detail.value.cpny_name;
    const cpny_location = event.detail.value.cpny_location;
    const cpny_code = event.detail.value.cpny_code;
    const cpny_man = event.detail.value.cpny_man;
    const cpny_phone = event.detail.value.cpny_phone;
    const cpny_mail = event.detail.value.cpny_mail;
    const cpny_profile = event.detail.value.cpny_profile;
    const cpny_deadline = event.detail.value.cpny_deadline;
    const cpny_password1 = event.detail.value.cpny_password1;
    const cpny_password2 = event.detail.value.cpny_password2;
    if (cpny_name == "") {
      getApp().tos("企业名称不能为空");
      return
    }


    if (this.data.temImgs.length < 1) {
      getApp().tos("未选择图片");
      return
    }
    if (cpny_password1 != cpny_password2) {
      getApp().tos("密码不一致");
      return
    }

    this.setData({
      regBtn: !this.data.regBtn
    })
    // this.upLoadImg();
    db.collection("company").where({
      'company_obj.cpny_mail': _.eq(cpny_mail)
    }).get().then(res => {
      
      if (res.data.length > 0) {

        that.setData({
          regBtn: !that.data.regBtn
        })
        getApp().tos("账号已存在")
        return


      } else {
        wx.showLoading({
          title: '上传中...'
        })

        

        const fileIDList = [];
        const today = new Date();
        const year = today.getFullYear();
        const mouth = today.getMonth() + 1;
        const day = today.getDate();
        let mouth_str='';
        let day_str='';
        if(mouth<10){
         mouth_str="0"+mouth;
        }else{ 
          mouth_str=mouth;
        }
        if(day<10){
          day_str="0"+day;
         }else{ 
           day_str=day;
         }
       
        that.data.temImgs.forEach((value, index) => {
          wx.cloud.uploadFile({
            filePath: value,
            cloudPath: "zzImg/" + year + "/" + mouth_str + "/"
              //+day+"/"
              +
              getUUID() + "." + getExt(value),
            success: res => {
              fileIDList.push(res.fileID);
              // if(fileIDList.length==that.data.temImgs.length){
                let company_obj = that.data.company_obj;
                company_obj.cpny_type = -1;
                company_obj.cpny_name = cpny_name;
                company_obj.cpny_location = cpny_location;
                company_obj.cpny_code = cpny_code;
                company_obj.cpny_man = cpny_man;
                company_obj.cpny_phone = cpny_phone;
                company_obj.cpny_mail = cpny_mail;
                company_obj.cpny_profile = cpny_profile;
                company_obj.cpny_deadline = cpny_deadline;
                company_obj.cpny_name = cpny_name;
                company_obj.cpny_password = cpny_password2;
                company_obj.images = fileIDList;
                company_obj.cpny_regist_date = year + "-" + mouth_str + "-" + day_str;

                wx.cloud.callFunction({
                  name: 'companyObjSave',
                  data: {
                    company_obj: company_obj
                  },
                  success: res => {
                    const _id = res.result._id; 
                    if (_id) {
                      wx.cloud.callFunction({
                        name: 'notice',
                        data: {
                          target: 'oAuf35SpOxe24BaPIPUZ4_2wDRXU',
                          title: "企业注册",
                          content: cpny_name,
                          note:  cpny_name
                        }
                      })
                      that.setData({
                        regBtn: !that.data.regBtn
                      })
                      wx.hideLoading();
                      wx.showToast({
                        icon: 'success',
                        
                        title: '上传成功!'
                      });
                      setTimeout(function () {
                        wx.navigateBack({})
                      }, 1000)
                    } else {
                      that.setData({
                        regBtn: !that.data.regBtn
                      })
                      wx.showToast({
                        title: '失败'
                      })
        
                    }
        
                  }
                })
            }
          })

        })

       

       
      }
    })







  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})